package org.hgvc.webapp.ets.jsf.beans.member;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;



public class MemberAgreementMgmtBean extends AbstractBean{

	public static final String BEAN_NAME="memberAgreementMgmtBean";
	public static final String NAV_LIST="memberAgreementMgmt";
	
	private MemberService service;
	private List<Agreement> agreementList;
	private Agreement agreement;
	private Member member;
	
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public List<Agreement> getAgreementList() {
		if(member==null){
			MemberPortalBean bean = (MemberPortalBean)FacesUtils.getManagedBean(MemberPortalBean.BEAN_NAME);
			member = bean.getMember();
		}
		try{
			agreementList = service.getAgreementListByMemberId(member.getMemberId());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return agreementList;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	
	public String navListAction(){
		return NAV_LIST;
	}
	
	public void navViewPointActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("agreementId");
		String id = (String)param.getValue();
		for(Agreement agreement:agreementList){
			if(agreement.getAgreementId().equalsIgnoreCase(id)){
				this.agreement = agreement;
				break;
			}
		}
	}
	
	public String navViewPointAction(){
		MemberAgreementPointEnquiryBean bean = (MemberAgreementPointEnquiryBean)FacesUtils.getManagedBean(MemberAgreementPointEnquiryBean.BEAN_NAME);
		return bean.navListAction(agreement);
	}
	
	public void navViewAgreementInvoiceActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("vagreementId");
		String id = (String)param.getValue();
		for(Agreement agreement:agreementList){
			if(agreement.getAgreementId().equalsIgnoreCase(id)){
				this.agreement = agreement;
				break;
			}
		}
	}
	
	public String navViewAgreementInvoiceAction(){
		MemberAgreementInvoiceBean bean = (MemberAgreementInvoiceBean)FacesUtils.getManagedBean(MemberAgreementInvoiceBean.BEAN_NAME);
		return bean.navListAction(agreement);
	}
}
